﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MebApp.Launcher
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //Debugger.Launch();
            Guid launchAppId = Guid.Empty;
            if (args.Length == 1)
            {
                Guid appId;
                var flag = ShortcutLaunchClient.LaunchByArguments(args[0], out appId);
                if (flag == ShortcutLaunchFlag.CmdSent)
                {
                    return;
                }
                else if (flag == ShortcutLaunchFlag.NoResponse)
                {
                    launchAppId = appId;
                }
            }
            TinyUtil.UpdateUtil.Setup();
            try
            {
                LauncherApp.Init(ConfigurationManager.AppSettings["ApiBaseUrl"]);
                LauncherApp.AppClient.Error += AppClient_Error;
                Helpers.RegEdit.RegProtocol("mebapp", Application.ExecutablePath);


                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FormMain(launchAppId));
            }
            catch (Exception ex)
            {
                LauncherApp.AppClient.AppBugReport(Guid.Empty, "MebAppLauncher", Application.ExecutablePath, ex);
                MessageBox.Show(ex.Message);
            }
        }

        private static void AppClient_Error(object sender, Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
}
